import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Colors from "../constants/Colors";
import { unitWidth } from '../constants/AdapterUtil';
import Picker from 'react-native-picker';

class TimePickerUtil {

    static myPicker;

    //计算当月天数
    static currentMonth(m, y) {
        var monthDay = 0;
        switch (m) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12: monthDay = 31; break;
            case 4:
            case 6:
            case 9:
            case 11: monthDay = 30; break;
            case 2:
                if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
                    monthDay = 29;

                } else {
                    monthDay = 28;
                }
        }
        return monthDay;
    };

    static dates(callback) {
        let date = new Date();
        let y = date.getFullYear() - 100;
        let m = String(date.getMonth() + 1);
        let d = String(date.getDate());
        let data = [];
        let year = null;
        let month = null;
        let maxY = y + 200;
        let minY = y;
        let minM = date.getMonth() + 1;
        let minD = date.getDate();
        for (let i = minY; i <= maxY; i++) {
            year = new Object();
            year[i] = [];
            let a;
            i === minY ? a = minM : a = 1;
            for (let j = a; j <= 12; j++) {
                month = new Object();
                month[j] = [];
                let monthDay = this.currentMonth(j, i);
                let day = [];
                let b;
                j === minM ? b = minD : b = 1;
                for (let k = b; k <= monthDay; k++) {
                    month[j].push(k);
                }
                year[i].push(month);
            }
            data.push(year);
        }
        this.pickerInit(data, [date.getFullYear(), m, d], '日期选择', callback);
    };

    //组件初始化
    static pickerInit(data, selectedValue, title, callback) {
        Picker.init({
            pickerData: data,
            selectedValue: selectedValue,
            pickerTitleText: title,
            pickerConfirmBtnText: '确定',
            pickerCancelBtnText: '取消',
            //确定
            onPickerConfirm: data => {
                data = data.join('-');
                callback.success(data)
                console.log(data)
            },
            //取消
            onPickerCancel: data => {
                console.log(data);
            },
            //选择
            onPickerSelect: data => {
                console.log(data);
            }
        });
        TimePickerUtil.myPicker = Picker;
        TimePickerUtil.myPicker.show();
    }
}

export default TimePickerUtil;